package com.example.bean;

import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Region;

import com.example.utils.DrawStyleUtil;

import java.util.ArrayList;

/**
 * 样式
 */
public class Mystyle {
    /**
     * 圆的半径
     */
    public static int yuan_width= DrawStyleUtil.yuan_banjin;
    /**
     *样式的类型
     */
    public int index;
   public ArrayList<Point> list=new ArrayList<Point>();

    /**
     * 样式颜色
     */
   private int color=0xff4385F3;
   private int line_width=2;
    private int xuxian_width=0;
    /**
     * 编辑时选中点的索引
     */
   private int select;
    /**
     * 1 选中圆  2选中矩形区域
     */
   public int selectState;

    public int getXuxian_width() {
        return xuxian_width;
    }

    public void setXuxian_width(int xuxian_width) {
        this.xuxian_width = xuxian_width;
    }

    /**

     * 判断这个点是否在这个样式上
     * @param x
     * @param y
     * @return 0 不在这个样式上  1在这个样式上在圆上 ，2在路径上的区域上
     */
   public int isOn(float x,float y)
   {
       //判断是否在圆上面
       if(index!=4&&index!=3) {
           for (int i = 0; i < list.size(); i++) {
               int a = isYuan(x, y, list.get(i));

               if (a == 1) {
                   select = i;
                   selectState = 1;
                   return a;
               }
           }
       }
       //竖线
       if(index==4)
       {
           if(Math.abs(x-list.get(0).getX())<=yuan_width)
           {
               selectState=2;
               return 2;
           }
       }
       for(int i=0;i<list.size()-1;i++)
       {
           Path path=new Path();
           path.moveTo(list.get(i).getX(),list.get(i).getY()+yuan_width);
           path.lineTo(list.get(i).getX(),list.get(i).getY()-yuan_width);
           path.lineTo(list.get(i+1).getX(),list.get(i+1).getY()-yuan_width);
           path.lineTo(list.get(i+1).getX(),list.get(i+1).getY()+yuan_width);
           path.lineTo(list.get(i).getX(),list.get(i).getY()+yuan_width);

           boolean a=pointInPath(path,(int)x,(int)y);
           if(a)
           {
               selectState=2;
               return 2;
           }
       }

       return 0;
   }
   private int isYuan(float x,float y,Point point)
   {
       float xa=x-point.getX();
       float ya=y-point.getY();
       float xy2=Math.abs(xa*xa)+Math.abs(ya*ya);
       int a=(int)Math.sqrt(xy2);
       if(a<=yuan_width)
       {
           return 1;
       }
       return 0;
   }
    private boolean pointInPath(Path path, int x,int y) {
        RectF bounds = new RectF();
        path.computeBounds(bounds, true);
        Region region = new Region();
        region.setPath(path, new Region((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom));
        return region.contains(x,y);
    }


    public int getSelect() {
        return select;
    }

    public void setSelect(int select) {
        this.select = select;
    }

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
    }

    public int getSelectState() {
        return selectState;
    }

    public void setSelectState(int selectState) {
        this.selectState = selectState;
    }

    public int getLine_width() {
        return line_width;
    }

    public void setLine_width(int line_width) {
        this.line_width = line_width;
    }
}
